//
//  SystemMessageViewMultiple.swift
//  甜言蜜语
//
//  Created by 王剑伟 on 16/6/21.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit

class SystemMessageViewMultiple: UIView {

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var timeLbl: UILabel!
    var delegate: SystemMessageDelegate?
    
    var modelList: [SystemMessageModel]? {
        didSet {
            self.timeLbl.text = modelList![0].ptime
            var frame = self.frame
            
            frame.size.height = CGFloat(158 + 101 * (modelList!.count - 1) + 60)
            
            self.frame = frame
            
            tableView.reloadData()
        }
    }
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
    override func awakeFromNib() {
        super.awakeFromNib()
        
        tableView.delegate = self
        tableView.dataSource = self

        tableView.registerNib(UINib(nibName: "SystemMessageBigPictureTableViewCell", bundle: nil), forCellReuseIdentifier: "SystemMessageBigPictureTableViewCell")
        tableView.registerNib(UINib(nibName: "SystemMessageNormalTableViewCell", bundle: nil), forCellReuseIdentifier: "SystemMessageNormalTableViewCell")
    }

    static func create(model: [SystemMessageModel]) -> SystemMessageViewMultiple {
        let view = NSBundle.mainBundle().loadNibNamed("SystemMessageViewMultiple", owner: nil, options: nil).last as! SystemMessageViewMultiple
        
        view.modelList = model
        
        return view
    }
}

extension SystemMessageViewMultiple: UITableViewDataSource {
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if let count = modelList?.count {
            return count
        }
        
        return 0
    }
    
    func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCellWithIdentifier("SystemMessageBigPictureTableViewCell", forIndexPath: indexPath) as! SystemMessageBigPictureTableViewCell
            
            cell.model = self.modelList![indexPath.row]
            cell.selectionStyle = UITableViewCellSelectionStyle.None
            return cell
        } else {
            let cell = tableView.dequeueReusableCellWithIdentifier("SystemMessageNormalTableViewCell", forIndexPath: indexPath) as! SystemMessageNormalTableViewCell
            cell.selectionStyle = UITableViewCellSelectionStyle.None
            cell.model = self.modelList![indexPath.row]
            
            return cell
        }
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        delegate?.onClick(modelList![indexPath.row].id)
    }
}

extension SystemMessageViewMultiple: UITableViewDelegate {
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if indexPath.row == 0 {
            return 158
        }
        
        return 101
    }
}
